package com.elegant.code.spi.web.gateway;

import com.elegant.code.spi.web.gateway.plugins.Plugin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Configuration
@Slf4j
public class GatewayConfiguration {
    @Bean("webHandler")
    public GatewayHandler WebHandler(final ObjectProvider<List<Plugin>> plugins) {
        final List<Plugin> pluginList = plugins.getIfAvailable(Collections::emptyList).stream()
                .sorted(Comparator.comparingInt(Plugin::getOrder)).collect(Collectors.toList());
        pluginList.forEach(Plugin -> log.info("loader plugin:[{}] [{}]", Plugin.named(), Plugin.getClass().getName()));
        return new GatewayHandler(pluginList);
    }

}
